package org.jeecg.modules.bairong;
import com.zhaodui.fxy.StringUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import net.sf.json.JSONObject;
@RestController
@RequestMapping("/bairong")
@Slf4j
public class bairongControler {
    @Value(value = "${jeecg.path.upload}")
    private String uploadpath;
    @Autowired
    RedisUtil redisUtil;

    @Value(value = "${bairong.common.apicode}")
    private String apicode;
    @Value(value = "${bairong.common.userName}")
    private String userName;
    @Value(value = "${bairong.common.password}")
    private String password;
    @Value(value = "${bairong.common.loginname}")
    private String loginname;


    @ApiOperation(value = "brpdfdownload")
    @RequestMapping(value = "/brpdfdownload",method = RequestMethod.GET)
    @ResponseBody
    public Result<?> siwftNumber(@RequestParam(value="swiftnumber", required=false)     String swiftnumber,
                                 @RequestParam(value="appCode", required=false)  String appCode
            ,                    @RequestParam(value="LoginApi", required=false)  String LoginApi) {
        // 验证String LoginApi ,
        String dbpath = "";
        String ctxPath = uploadpath;
        String fileName = swiftnumber+".pdf";
        String bizPath = "bairong";
        String nowday = new SimpleDateFormat("yyyyMMdd").format(new Date());
        File file = new File(ctxPath + File.separator + bizPath + File.separator + nowday);
        if (!file.exists()) {
            file.mkdirs();// 创建文件根目录
        }
        String savePath = file.getPath() + File.separator + fileName;
        dbpath = bizPath + File.separator + nowday + File.separator + fileName;
        try{
            String tokenid = bairongUtil.getTokenid(LoginApi,apicode,userName,password);
            String result = bairongUtil.downloadPdf(apicode, tokenid, swiftnumber,savePath);
            System.out.println("result===="+result);
            Result.ok(dbpath);
        }catch (Exception e){
            Result.error(e.getMessage());
        }
        return Result.ok(dbpath);
    }

    @ApiOperation(value = "apiquery")
    @RequestMapping(value = "/apiquery",method = RequestMethod.GET)
    @ResponseBody
    public Result<?> daiqian(@RequestParam(value="apiName", required=true)     String apiName,
                             @RequestParam(value="strategyid", required=false)     String strategyid,
                             @RequestParam(value="conf_id", required=false)     String confid,
                             @RequestParam(value="id", required=true)     String id,
                             @RequestParam(value="cell", required=true)     String cell,
                             @RequestParam(value="name", required=true)     String name,
                                 @RequestParam(value="appCode", required=false)  String appCode,
                             @RequestParam(value="LoginApi", required=false)  String LoginApi) {
        // 验证
        String result = "";
        JSONObject jsonData = new JSONObject();
        JSONObject reqData = new JSONObject();
        String tokenid = bairongUtil.getTokenid(LoginApi,apicode,userName,password);
        //接口地址(apiName传以下参数)---
        //可以使用正式环境接口地址  LoginApi(正式环境)   SandboxLoginApi(测试环境)
        //	贷前策略地址：strategyApi(正式环境)		SandboxstrategyApi(测试环境)
        //	验证策略地址：verificationApi (正式地址)	SandboxverApi(测试地址)
        //注：1.测试过程可以使用正式地址
//        MCP0030079
//                验证001
//        STR0030078
//                贷前001
        //   2.apiName环境要保持与loginName环境一致,正式都是正式、测试都是测试,不可以交叉使用.
        //   3.在调用测试环境时需要事先联系百融技术并提供apicode及策略编号（策略编号请查看邮件里的策略配置表）
        jsonData.put("apiName",apiName);
        jsonData.put("tokenid",tokenid);
        //贷前的策略编号(请查看策略配置表)
        if(StringUtil.isNotEmpty(strategyid)){
            reqData.put("strategy_id",strategyid);
        }
        //贷前的策略编号(请查看策略配置表)
        if(StringUtil.isNotEmpty(confid)){
            reqData.put("conf_id",confid);
        }
        //请求参数 id cell name (如产品文档有其他必传参数按照文档为主)
        reqData.put("id",id);
        reqData.put("cell",cell);
        reqData.put("name",name);
        jsonData.put("reqData",reqData);
        System.out.println("请求参数:"+jsonData.toString());
        result = bairongUtil.getBrData(tokenid,jsonData.toString());
        return Result.ok(result);
    }

}
